<%@page import="net.tanesha.recaptcha.ReCaptchaResponse"%>
<%@page import="net.tanesha.recaptcha.ReCaptchaImpl"%>
<%@page import="java.security.MessageDigest"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="conectar.Conectar"%>
<%@page session="true" %>
<%
    
    
    
        String remoteAddr = request.getRemoteAddr();
        ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
        reCaptcha.setPrivateKey("6Lf38OISAAAAAOkaaWnubQH7HRzWvlyKPmFP5RUi");

        String challenge = request.getParameter("recaptcha_challenge_field");
        String uresponse = request.getParameter("recaptcha_response_field");
        ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);

//        if (reCaptchaResponse.isValid()) {
//          out.print("Answer was entered correctly!");
//          //HttpSession sesionOk = request.getSession();
//        //sesionOk.setAttribute("usuario", usuario);
//        response.sendRedirect("correcto.jsp");
//        } else {
//          out.print("Answer is wrong");
//        }
      
      
    String usuario = "";
    String password = "";
     
    if (request.getParameter("usuario") != null) {
        usuario = request.getParameter("usuario");
    }
        
    if (request.getParameter("clave") != null) {
        password = request.getParameter("clave");
    }
    
           // -----------------------INICIO MD5 ------------------------------
       
        Seguridad.Funciones p= new Seguridad.Funciones();
        password=p.md5(password);
            // -----------------------FIN MD5 ------------------------------
    
    try {
        conectar.Conectar miconexion = new Conectar();
        String sql = "SELECT *  FROM votante WHERE  PersonaIFE = "+usuario+" AND password='"+ password +"'"; 
        //String sql = "SELECT *  FROM `votante` WHERE `PersonaIFE` = " + usuario + " AND `password` = '" + password + "'";
        ResultSet consulta = miconexion.consulta(sql);
        
        if (consulta.next()) 
            {
               if (usuario.equals(consulta.getString(1)) && password.equals(consulta.getString(2)) && reCaptchaResponse.isValid())
                    {

                        HttpSession sesionOk = request.getSession();
                        sesionOk.setAttribute("usuario", usuario);

%>
<jsp:forward page="menu.jsp" />
<%

}
else if (usuario.equals(consulta.getString(1)) && password.equals(consulta.getString(2)))
{

HttpSession sesionOk = request.getSession();
sesionOk.setAttribute("usuario", usuario);

%>
<jsp:forward page="login.jsp">
    <jsp:param name="error" value="Ingrese el captcha correcto."/>
</jsp:forward>
<%

}
}
else {
%>

<jsp:forward page="login.jsp">
    <jsp:param name="error" value="Usuario y/o clave incorrectos.<br>Vuelve a intentarlo."/>
</jsp:forward>
<%    
  miconexion.desconectar();
}
          
       
} 
    
catch (Exception e) {
out.println(e.getMessage());
}


%>